<?php
    session_start();
    if (!isset($_SESSION['User'])) {
        require ("login.php");
        exit;
    }
    //$title is the page title
    $title = "Dashboard";
    require('includes/header.php');

?>

        <script type="text/javascript">
            $(document).ready(function()
            {
<?php

if ($_SESSION['Admin'] == 1)
echo ("$('#siteadmin').load('siteadmin.php');");

if ($_SESSION['Publisher'] == 1)
echo ("$('#publisher').load('unapprovedpages.php');");

if ($_SESSION['Author'] == 1)
echo ("$('#author').load('author.php');");
?>

        //After AJAX request done loading
        $('#main').ajaxComplete(function() {
            //initially hide the error and success messages
            $('.pub_success').hide();
            $('.pub_error').hide();
            $('.admin_success').hide();
            $('.admin_error').hide();

            //if the approve or deny button is clicked
            $(".action").click(function() {
                //declare datastring and other variables
                var dataString = "";
                var admin;
                var pub;
                var author;

                //approval or denial?
                var action = $(this).val();
                
                //for which record?
                var id = $('#record').val();

                //initialize datastring
                dataString += "record=" + id + "&";
                
                //check the value of all permission checkboxes
                $('#admin'+id).each(function(){
                    if (this.checked)
                        admin = 1;
                    else admin = 0;
                })
                $('#pub'+id).each(function(){
                    if (this.checked)
                        pub = 1;
                    else pub = 0;
                })
                $('#author'+id).each(function(){
                    if (this.checked)
                        author = 1;
                    else author = 0;
                })

                //if no permission boxes are checked and approve button is clicked
                // show an error message
                if(action == "Approve" && admin == 0 && author == 0 && pub == 0 )
                {
                    $('.admin_error').fadeIn(200).show();
                }
                
                else
                {
                    //set datastring for approvals
                    if (action == "Approve")
                    {                       
                        dataString += "admin=" + admin + "&pub=" + pub + "&author=" + author +"&";
                    }
       
                    //add the action button info to the datastring
                    dataString += "action="+action;
                    
                    //submit the form with the XMLHTTPRequest
                    $.ajax({
                        type: "POST",
                        url: "siteadmin.php",
                        data: dataString,
                        success: function(){
                            $('.admin_success').show();
                        }
                    });

                    //reload the widget with any changes
                    $('#siteadmin').load('siteadmin.php');                   
                }
                return false;
            });

            //this handles publishing forms on the approve pages widget
                $(".publish").click(function() {
                //if no boxes are checked, show an error message
                if( $("input[name=publish\\[\\]]:checked").val() == null)
                {
                    $('.pub_error').fadeIn(200).show();
                }

                else
                {
                    
                    //initialize dataString
                    var dataString = "";

                    //walk through every published checkbox and set
                    //dataString
                    $("input[name=publish\\[\\]]").each(function()
                    {
                        if (this.checked)
                            dataString += "publish[]=" + this.value + "&";
                    })

                    //add the button info to the datastring
                    dataString += "submit=Publish+Selected";
                    //alert (dataString);
                    //submit the form with the XMLHttpRequest
                    $.ajax({
                        type: "POST",
                        url: "unapprovedpages.php",
                        data: dataString,
                        success: function(){
                            $('.pub_success').show();

                        }
                    });

                    //reload the widget(s) with any changes
                    $('#publisher').load('unapprovedpages.php');
                }
                return false;
            });
        });

    });

        </script>

      <?php require('includes/sidebar.php'); ?>
    
        <div id="main">
        <h2>Dashboard</h2>
            <div id="errmessages">
                <?php
                if ($_GET['msg'] == "accessdenied")
                {
                    echo ("You do not have the permissions to view that page!");
                }

                elseif ($_GET['msg'] == "waitingapproval")
                {
                    echo ("Your page is waiting to be approved and cannot be edited!");
                }


                elseif ($_GET['msg'] == "errcreating")
                {
                    echo ("Error creating page!");
                }
                ?>
            </div>

            <div id="messages">
                <?php
                if ($_GET['msg'] == "pagecreated")
                {
                    echo ("Your page has been created and is waiting to be approved.");
                }
                ?>
            </div>
            <div id="siteadmin"> </div>
            <div id="publisher"> </div>
            <div id="author"> </div>
        </div>
        
    <?php require('includes/footer.php'); ?>
   
